package com.starter.home.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.starter.home.domain.entity.User;
import com.starter.home.util.ResponseResult;
import com.starter.home.util.constant.UserConst;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 菜单控制器
 * @author
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @GetMapping(value = "/getList")
    public ResponseResult getMenu(HttpSession session) throws FileNotFoundException {
        User user = (User) session.getAttribute("user");
        if (StringUtils.equals(UserConst.ADMIN,user.getAccount())){
            File jsonFile = ResourceUtils.getFile("classpath:data/menu-admin.json");
            String s = FileUtil.readUtf8String(jsonFile);
            JSONArray jsonArray = JSON.parseArray(s);
            return ResponseResult.success(jsonArray);
        }
        File jsonFile = ResourceUtils.getFile("classpath:data/menu-user.json");
        String s = FileUtil.readUtf8String(jsonFile);
        JSONArray jsonArray = JSON.parseArray(s);
        return ResponseResult.success(jsonArray);

    }

}
